package com.gulimall.authserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gulimall.authserver.feign.MemberFeignService;
import com.gulimall.authserver.vo.SocialUser;
import com.run.common.utils.HttpUtils;
import com.run.common.utils.R;
import com.run.common.vo.MemberRespVo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Date: 2022/02/08/11:41
 * @Description:
 */
@Controller
public class Oauth2Controller {

    @Autowired
    MemberFeignService memberFeignService;

    @GetMapping("/oauth2.0/gitee/success")
    public String gitee(@RequestParam String code, HttpSession session) throws Exception {
        Map<String,String> map = new HashMap<>();
        map.put("client_id","66e1f3b24003a2bb63c6c7c371d9e3cf69159e625b616ffab3fac75d8e3c8fcf");
        map.put("client_secret","93a7c902291458c5184c2bb4624f2014932c29a4cf371bf34e58c7545673a510");
        map.put("redirect_uri","http://auth.gulimall.com/oauth2.0/gitee/success");
        map.put("grant_type","authorization_code");
        map.put("code",code);
        //1、根据code换取accessToken
        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token",map);
        //处理
        if (response.getStatusLine().getStatusCode() == 200){
            //获取到了 accessToken
            String json = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
            String access_token = socialUser.getAccess_token();
            HashMap<String, String> headerMap = new HashMap<>();
            headerMap.put("access_token",access_token);
            HttpResponse responseInfo = HttpUtils.doGet("https://gitee.com", "/api/v5/user", headerMap);
            //获取到基本信息
            if (responseInfo.getStatusLine().getStatusCode() == 200){
                String s = EntityUtils.toString(responseInfo.getEntity());
                Map parseObject = JSON.parseObject(s, Map.class);
                socialUser.setId((Integer) parseObject.get("id"));
                socialUser.setName((String) parseObject.get("name"));
                //进行社交登录
                R oauthlogin = memberFeignService.oauthlogin(socialUser);
                if (oauthlogin.getCode() == 0){
                    //登录成功
                    MemberRespVo data = (MemberRespVo)oauthlogin.getData("data", new TypeReference<MemberRespVo>() {});
                    session.setAttribute("loginUser",data);
                    return "redirect:http://gulimall.com";
                }
            }
        }
        return "redirect:http://auth.gulimall.com/login.html";
    }


}
